package ClientUI;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DebugGraphics;

import javax.swing.JButton;

import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import Protocol.noticeMsgWord.NoticeMsgWord;
import Protocol.noticeMsgWord.NoticeMsgWord_R;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RealMsgPanel extends javax.swing.JPanel {
	private JButton publishButton;
	private JLabel descLabel;
	private JTextField tittleField;
	private JPanel editPanel;
	private JSeparator jSeparator1;
	private JLabel statusLabel;
	private JLabel tittleLabel;

	private ClientGUI		clientGui;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JPanel feedbackPanel;
	private JTextArea editArea;
	
	private NoticeMsgWord	notice;
	private ArrayList<JTextArea>	 feedbackList = new ArrayList<JTextArea>();

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	/*public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new RealMsgPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}*/
	
	public RealMsgPanel(ClientGUI  cgui) {
		super();
		this.clientGui = cgui;
		initGUI();
		addMyListeners();
	}
	public void updateAllMsg(ArrayList<NoticeMsgWord>  msgList){
		
	}
	public void addNotice(NoticeMsgWord no) {
		SwingUtilities.invokeLater(new AddNoticeRunnable(no));
	}
	class AddNoticeRunnable implements Runnable {
		NoticeMsgWord	 nmw;
		AddNoticeRunnable(NoticeMsgWord no) {
			nmw = no;
		}
		public void run() {
			addNoticePrivate(nmw);
		}
	}
	class MyTextArea extends JTextArea {
		NoticeMsgWord	notice;
		public MyTextArea(NoticeMsgWord no) {
			this.notice = no;
			this.setSize(feedbackPanel.getWidth()-20, feedbackPanel.getHeight()/4);
			this.setText("tittle: "+no.getTitle()+"\ncontent: "+no.getBody());
			this.setEditable(false);
		}	
	}
	private void addNoticePrivate(NoticeMsgWord	no) {
		JTextArea	tmp = new MyTextArea(no);
		JScrollPane pane = new JScrollPane();
		pane.setViewportView(tmp);
		pane.setSize(tmp.getWidth(), tmp.getHeight());
		tmp.setVisible(true);
		pane.setVisible(true);
		/*tmp.setSize(feedbackPanel.getWidth()-20, feedbackPanel.getHeight()/4);
		tmp.setText(no.getTitle()+":\n"+no.getContent());*/
		this.feedbackList.add(tmp);
		feedbackPanel.add(pane);
		this.feedbackPanel.repaint();
		this.clientGui.mainFrame.repaint();
	}
	/*private void exitPublish() {
		this.statusLabel.setText("success !");
		this.statusLabel.setVisible(true);
		addNotice(notice);
	}*/
	private void handlePublishButton() {
		notice = new NoticeMsgWord();
		NoticeMsgWord_R  nr = new NoticeMsgWord_R();
		
		notice.setBody(this.editArea.getText());
		notice.setCategory("notice");
		notice.setFrom_id(clientGui.getClient().getClientData().getSelf_basic().getUser_id());
		notice.setTitle(this.tittleField.getText());
		ArrayList<NoticeMsgWord>    noticeList = new ArrayList<NoticeMsgWord>();
		nr.setChatMsgList(noticeList);
		notice.setTo_id("all");
		
		clientGui.getClient().getClientNet().sendMsgTo(clientGui.getClient().getClientData().getServer_basic(), nr);
		
		addNotice(notice);
		/*SwingUtilities.invokeLater(new Runnable(){
			public void run() {
				exitPublish();
			}
		});*/
	}
	private void addMyListeners() {
		this.publishButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				handlePublishButton();
			}
			
		});
	}
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(427, 484));
			this.setLayout(null);
			this.setSize(420, 445);
			{
				jSeparator1 = new JSeparator();
				this.add(jSeparator1);
				jSeparator1.setBounds(6, 256, 407, 10);
			}
			{
				editPanel = new JPanel();
				this.add(editPanel);
				editPanel.setBounds(24, 272, 367, 161);
				editPanel.setLayout(null);
				editPanel.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				{
					tittleField = new JTextField();
					editPanel.add(tittleField);
					tittleField.setText("your tittle here");
					tittleField.setBounds(52, 11, 308, 24);
				}
				{
					tittleLabel = new JLabel();
					editPanel.add(tittleLabel);
					tittleLabel.setText("Tittle");
					tittleLabel.setBounds(12, 14, 28, 17);
				}
				{
					descLabel = new JLabel();
					editPanel.add(descLabel);
					descLabel.setText("input your announcement here");
					descLabel.setBounds(14, 37, 198, 17);
				}
				{
					statusLabel = new JLabel();
					editPanel.add(statusLabel);
					statusLabel.setBounds(14, 129, 227, 18);
					statusLabel.setText("failed !");
					statusLabel.setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION);
					statusLabel.setHorizontalTextPosition(SwingConstants.CENTER);
					statusLabel.setVisible(false);
				}
				{
					publishButton = new JButton();
					editPanel.add(publishButton);
					publishButton.setText("Publish");
					publishButton.setBounds(271, 127, 76, 24);
				}
				{
					jScrollPane1 = new JScrollPane();
					editPanel.add(jScrollPane1);
					jScrollPane1.setBounds(24, 60, 323, 57);
					jScrollPane1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
					jScrollPane1.setOpaque(false);
					{
						editArea = new JTextArea();
						jScrollPane1.setViewportView(editArea);
						editArea.setText("your notice here");
						editArea.setBounds(12, 55, 344, 101);
						editArea.setPreferredSize(new java.awt.Dimension(302, 59));
					}
				}
			}
			{
				jScrollPane2 = new JScrollPane();
				this.add(jScrollPane2);
				jScrollPane2.setBounds(12, 12, 393, 226);
				{
					feedbackPanel = new JPanel();
					BoxLayout feedbackPanelLayout = new BoxLayout(feedbackPanel, javax.swing.BoxLayout.Y_AXIS);
					jScrollPane2.setViewportView(feedbackPanel);
					feedbackPanel.setBounds(12, 12, 393, 226);
					feedbackPanel.setLayout(feedbackPanelLayout);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
